﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace CloseWindow
{
    public partial class Warning : Form
    {
        #region Attributes

        int maxTimeNumber = 60; //定义最大定时时间60秒
        Form1 objShutDown;  //定义主窗体类型变量
        ExitWindows objExitWindows;

        #endregion

        //在构造函数中传递主窗体对象给objShutDown
        public Warning(Form1 mainForm)
        {
            InitializeComponent();
            objShutDown = mainForm;
        }

        private void Warning_Load(object sender, EventArgs e)
        {
            this.timer1.Start();
            this.label1.Text = "离自动关机还有60秒，请及时保存资料";
        }

        private void Warning_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.timer1.Stop();
            timer1.Dispose();
            objShutDown.Close();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.maxTimeNumber = maxTimeNumber - 1;
            if (this.maxTimeNumber == 0)
            {
                this.timer1.Stop();
                objExitWindows = new ExitWindows();
                bool ok = objExitWindows.ShutDown();

                if (!ok)
                {
                    MessageBox.Show("无法完成操作");
                }
            }
            else
            {
                this.label1.Text = "离自动关机还有" + this.maxTimeNumber.ToString() + "秒,请及时保存资料";
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.timer1.Stop();
            timer1.Dispose();
            this.Hide();
            objShutDown.lblReserve.Text = "任务已取消";
            objShutDown.btnStart.Enabled = true;
            objShutDown.WindowState = FormWindowState.Normal;
            objShutDown.Visible=true;
        }
    }
}
